package id.co.rho.scoring.cid.shared.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import id.co.rho.scoring.cid.changecontrol.entitychange.UpdateEntityChange;
import id.co.rho.scoring.cid.client.model.ChangeTrackingModel;

public class FunctionGroupModel extends ChangeTrackingModel{
	private static final long serialVersionUID = 1L;
	public static final String ID = "id";
	public static final String NAME = "functGrpName";
	public static final String STATUS = "functGrpStatus";
	public static final String DESC = "functGrpDesc";
	public static final String FUNCTIONS = "functions";
	private List<AppFunctionModel> functionList; 
	
	public FunctionGroupModel() {
		super();
	}
	
	public FunctionGroupModel(UpdateEntityChange change) {
		super(change);
	}
	
	public static List<FunctionGroupModel> applyChanges(List<FunctionGroupModel> modelList, List<UpdateEntityChange> uecList, List<AppFunctionModel> allAppFunctionList){
    	Long idModel, idUec;
    	FunctionGroupModel newModel;
    	if(uecList!=null){
	    	for (UpdateEntityChange change : uecList) {
				idUec = (Long) change.getEntityId();
				if(idUec!=null){
			    	for (FunctionGroupModel model : modelList) {
			    		idModel = (Long) model.get(LitigationModel.ID);    		
		    			if(idModel.compareTo(idUec)==0){
		    				model.setChange(change);
		    				if(change.getRecordedChange(FUNCTIONS)!=null){
								Set idFunctions = (Set) change.getRecordedChange(FUNCTIONS).getNewValue();
								model = applyChangesSet(model,idFunctions,allAppFunctionList);
							}
		    			}
		    		}
				}else{
					newModel = new FunctionGroupModel(change);
					if(change.getRecordedChange(FUNCTIONS)!=null){
						Set idFunctions = (Set) change.getRecordedChange(FUNCTIONS).getNewValue();
						newModel = applyChangesSet(newModel,idFunctions,allAppFunctionList);
					}
					modelList.add(newModel);
				}
				
	    	}
    	}
    	return modelList;
    }
	
	public static FunctionGroupModel applyChangesSet(FunctionGroupModel fgm,Set idFunctions, List<AppFunctionModel> allFunctionList){
		fgm.setFunctionList(fromSetToList(allFunctionList, idFunctions));
		return fgm;
	}

	public List<AppFunctionModel> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<AppFunctionModel> functionList) {
		this.functionList = functionList;
	}
	
	public void updateFunctionList(List<AppFunctionModel> newFunctionList) {
		set(FUNCTIONS, convertToIdSet(newFunctionList));
		this.functionList = newFunctionList;
	}
	
	public Set<Long> convertToIdSet(List<AppFunctionModel> list){
		Set<Long> set = new HashSet();
		if(list!=null){
			for(AppFunctionModel fg: list){
				set.add((Long) fg.get(AppFunctionModel.ID));
			}
		}
		return set;
	}
	
	public static List<AppFunctionModel> fromSetToList(List<AppFunctionModel> allList, Set<Long> idList){
		List<AppFunctionModel> newList = new ArrayList();
		AppFunctionModel mod = null;
		for(Long id : idList){
			mod = getFunctionFromListById(allList, id);
			if(mod!=null){
				newList.add(mod);
			}
		}
		return newList;
	}
	
	public static AppFunctionModel getFunctionFromListById(List<AppFunctionModel> allList, Long id){
		AppFunctionModel mod = null;
		for(AppFunctionModel model : allList){
			if(id.compareTo((Long) model.get(AppFunctionModel.ID))==0){
				mod = model;
			}
		}
		return mod;
	}
	
	public static List<String> getListAllFunctionCode(List<FunctionGroupModel> listRole){
		List functionCodeList = new ArrayList();
    	if(listRole!=null && listRole.size()>0){
    		for(FunctionGroupModel fg: listRole){
    			if(fg.getFunctionList()!=null && fg.getFunctionList().size()>0){
    				for(AppFunctionModel f : fg.getFunctionList()){
    					if(f.get(AppFunctionModel.FUNCTION_CODE)!=null){
    						functionCodeList.add(f.get(AppFunctionModel.FUNCTION_CODE));
    					}
    				}
    			}
    		}
    	}
    	return functionCodeList;
	}
}
